using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 使实体朝指定方向沿直线移动
    /// 参数组1:1.实体tag或mcid，2.移动距离（厘米），3.朝向x(厘米)，4.朝向z(厘米)
    /// </summary>
    public class ExecLinearMoveBySpecifiedDir : BaseServiceExecution
    {
        public override void Execute(LevelRegion region, LevelPlayer player)
        {
            var config = _config as ExecParamLinearMoveBySpecifiedDir;
            if (!config.tagOrMcid.TryGetValue(triggerEntity, out int tagOrMcid))
            {
                LogicLog.LogError("无法获取到tagOrMcid.");
                return;
            }
            int moveDis = config.moveDis;
            float dirX = config.dir.x;
            float dirZ = config.dir.z;

            LevelEntity entity = tagOrMcid == 0 ? triggerEntity : region.GetEntityByTagOrMcid(tagOrMcid);

            if (entity == null)
            {
                LogicLog.LogError($"找不到指定的实体，rag or id：{tagOrMcid}");
                return;
            }

            Vector3Logic stopPoint = entity.position + new Vector3Logic(dirX, 0, dirZ).normalized * moveDis;

            LgEntityLinearMove moveCom = entity.AddComponent<LgEntityLinearMove>();
            moveCom.MoveTo(stopPoint, 400, EntityMoveDirType.KeepRotation);
        }
    }
}
